package com.gary.rsa.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengrui
 * @date 2021/1/28 - 16:29
 */
@RestController
public class RSATestController {

    @GetMapping("/send")
    public String sendInfo() {
        RSA rsa = new RSA();
        System.out.println("私钥---"+rsa.getPrivateKey());
        System.out.println("私钥64---"+rsa.getPrivateKeyBase64());

        System.out.println("公钥---"+rsa.getPublicKey());
        System.out.println("公钥---"+rsa.getPublicKeyBase64());

        String data = "sfsfaodjf;oajf;sdjf;oasjf;sdjf;oasj;fasj;ofjoflghalkngfeafhglasdu" +
                "oirgasdhfuiawrg;sdhjfafsdngerughsoda;;foasjgiufdgetnhgvkbnaoitgaojdfsdg" +
                "jehtgdfjgnbkxjvnsdfasdfasdfsdfasdfsdfewrgsdfgsrthfvbnfgj561313515132421" +
                "5zfbzvcbfgj中国";
        //公钥加密
        byte[] encrypt = rsa.encrypt(data.getBytes(), KeyType.PublicKey);
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
        String okStr = StrUtil.str(decrypt, CharsetUtil.UTF_8);
        System.out.println("解密："+okStr);
        return "success";
    }
}
